package cn.wolfcode.p2p.web.controller;

import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.exception.CustomerException;
import cn.wolfcode.p2p.base.service.ILoginInfoService;
import cn.wolfcode.p2p.base.util.AssertUtil;
import cn.wolfcode.p2p.base.util.JSONResult;
import cn.wolfcode.p2p.base.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 后台:登录注销
 */
@Controller
public class LoginController {

    @Autowired
    private ILoginInfoService loginInfoService;

    // 登录 不能在地址栏访问Post
    @PostMapping("/login")
    @ResponseBody
    public Object login(String username, String password) {
        JSONResult result = new JSONResult();
        LoginInfo currentUser = loginInfoService.login(username, password, LoginInfo.TYPE_MANAGER);
        if (currentUser == null) {
            AssertUtil.INSTANCE.isNotNull(currentUser, "账号或密码错误");
        }

        return result;
    }

    // 注销 不能在地址栏访问Post
    @GetMapping("/logout")
    public String logout() {
        UserContext.setCurrentUser(null);
        return "redirect:/login.html";
    }

    // 登录成功跳转到main界面
    @RequestMapping("/main")
    public String toMain() {
        return "main";
    }

}
